package cn.eveo.trypagerindicator;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * 指示栏tab
 */
public class TabView extends LinearLayout {
    private Tab mTab;
    private TextView mTextView;
    private ImageView mIconView;
    private LayoutParams textParams;
    private LayoutParams iconParams;

    public TabView(Context context, int tabRule) {
        super(context);
        setClickable(true);
        setGravity(Gravity.CENTER);
        setOrientation(VERTICAL);
        mTextView = new TextView(context);
        mIconView = new ImageView(context);
        textParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        iconParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        mTextView.setSingleLine(true);
        mTextView.setLayoutParams(textParams);
        mIconView.setLayoutParams(iconParams);

        switch (tabRule) {
            case TryPagerIndicator.ICON_ABOVE_TEXT:
                addView(mIconView);
                addView(mTextView);
                break;
            case TryPagerIndicator.ICON_BELOW_TEXT:
                addView(mTextView);
                addView(mIconView);
                break;
        }
    }


    public ImageView getIconView() {
        return mIconView;
    }

    public TextView getTextView() {
        return mTextView;
    }

    public int getIndicatorLeft() {
        int left;
        if (mTab.isSame()) {
            left = getLeft() + mTextView.getLeft();
        } else {
            left = getLeft();
        }
        return left;
    }

    public int getIndicatorRight() {
        int right;
        if (mTab.isSame()) {
            right = getLeft() + mTextView.getRight();
        } else {
            right = getRight();
        }
        return right;
    }

    public void update(@Nullable Tab tab) {
        if (null == tab) {
            return;
        }
        this.mTab = tab;
        setPadding(mTab.getLeftPadding(), mTab.getTopPadding(), mTab.getRightPadding()
                , mTab.getBottomPadding());
        updateTextView();
        updateIcon();
    }

    private void updateIcon() {
        if (mIconView != null) {
            if (mTab.getIcon() != null) {
                mIconView.setImageDrawable(mTab.getIcon());
                mIconView.setVisibility(VISIBLE);
                setVisibility(VISIBLE);
            } else {
                mIconView.setVisibility(GONE);
                mIconView.setImageDrawable(null);
            }
        }
    }

    private void updateTextView() {
        if (null == mTextView) {
            return;
        }
        boolean hasText = !TextUtils.isEmpty(mTab.getText());
        if (hasText) {
            mTextView.setText(mTab.getText());
            mTextView.setVisibility(VISIBLE);
            setVisibility(VISIBLE);
        } else {
            mTextView.setVisibility(GONE);
            mTextView.setText(null);
        }

        mTextView.setTextColor(mTab.getTextColor());
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTab.getTextSize());
        if (mTab.isTextAllCaps()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                mTextView.setAllCaps(true);
            } else {
                mTextView.setText(mTextView.getText().toString().toUpperCase(mTab.getLocale()));
            }
        }
    }
}
